﻿// ***********************************************************************
// Assembly         : INNOVACALL.Framework.Common
// Author           : LJO+01 (Laurent Jordi)
// Created          : 06-05-2013
// Last Modified By : LJO+01 (Laurent Jordi)
// Last Modified On : 06-05-2013
// ***********************************************************************
// <copyright file="CommonLocalization.cs" company="Copyright © Innovacall 2011">
//     Copyright (c) Copyright © Innovacall 2011. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Web;
using System.Reflection;
using System.Web.Security;
using System.Globalization;
using System.Threading;

namespace INNOVACALL.Framework.Common.MVCHelpers.Localization
{
    /// <summary>
    /// Utilitaires pour les classes helpers de localisation
    /// </summary>
    public class CommonLocalization
    {
        /// <summary>
        /// The favorite language cookie name
        /// </summary>
        public static string FavoriteLanguageCookieName = "MyPrettyCMSUserFavoriteLanguage";
        /// <summary>
        /// It examines a resource for use in DisplayNameLocalizedAttribute
        /// Example of using a resource file to be called Titles.resx
        /// that contains two channels: CreationDateDisplayName and CreationDateDescription
        /// [Common.DisplayNameLocalized (typeof (Resources.Titles) "CreationDateDisplayName")
        /// Common.DescriptionLocalized (typeof (Resources.Titles) "CreationDateDescription")]
        /// public DateTime CreationDate
        /// {
        /// get;
        /// set;
        /// }
        /// </summary>
        /// <param name="resourceManagerProvider">The resource manager provider.</param>
        /// <param name="resourceKey">The resource key.</param>
        /// <returns>System.String.</returns>
        public static string LookupResource(Type resourceManagerProvider, string resourceKey)
        {
            string toReturn = resourceKey;
            //ToDo LJO
            //var filterContext = HttpContext.Current.Request.RequestContext;
            ////Fix threading culture issue
            //if (filterContext.RouteData.Values[LocalizationAttribute.cstRouteDataLangName] != null &&
            //    !string.IsNullOrWhiteSpace(filterContext.RouteData.Values[LocalizationAttribute.cstRouteDataLangName].ToString())
            //) {
            //    string routeLanguage = filterContext.RouteData.Values[LocalizationAttribute.cstRouteDataLangName].ToString();
            //    SetCurrentThreadLanguage(routeLanguage);
            //}
            //Très important, (BindingFlags.Static | BindingFlags.Public) si la ressource est publique, il faut utiliser BindingFlags.Public sinon BindingFlags.NonPublic
            foreach (PropertyInfo staticProperty in resourceManagerProvider.GetProperties(BindingFlags.Static | BindingFlags.Public))
            {
                if (staticProperty.PropertyType == typeof(System.Resources.ResourceManager))
                {
                    System.Resources.ResourceManager resourceManager = (System.Resources.ResourceManager)staticProperty.GetValue(null, null);
                    if (!string.IsNullOrEmpty(resourceKey))
                    {
                        toReturn = resourceManager.GetString(resourceKey);
                    }
                }
            }
            return toReturn;
        }

        /// <summary>
        /// Sets the current thread language.
        /// </summary>
        /// <param name="language">The language.</param>
        public static void SetCurrentThreadLanguage(string language)
        {
            HttpContext _httpContext = HttpContext.Current;

            var ci = CultureInfo.CreateSpecificCulture(language);
            Thread.CurrentThread.CurrentUICulture = ci;
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);

            _httpContext.Response.Cookies.Remove("Language");
            var langCookie = new HttpCookie("Language", ci.IetfLanguageTag);
            langCookie.Expires = System.DateTime.Now.AddDays(21);
            _httpContext.Response.Cookies.Add(langCookie);
        }
        /// <summary>
        /// Keeps the culture.
        /// </summary>
        /// <param name="culture">The culture.</param>
        /// <param name="uiCulture">The unique identifier culture.</param>
        /// <returns>CultureKeeper.</returns>
        public static CultureKeeper KeepCulture()
        {
            CultureKeeper toReturn = new CultureKeeper() { Culture = Thread.CurrentThread.CurrentCulture, UICulture = Thread.CurrentThread.CurrentUICulture };
            return toReturn;
        }

        /// <summary>
        /// Restaures the culture.
        /// </summary>
        /// <param name="keptCulture">The kept culture.</param>
        public static void RestaureCulture(CultureKeeper keptCulture)
        {
            Thread.CurrentThread.CurrentUICulture = keptCulture.UICulture;
            Thread.CurrentThread.CurrentCulture = keptCulture.Culture;
        }
    }
}
